<?php
/**
 * 城市模型
 *
 * PHP version 7
 *
 * @category  Business
 * @package   App\Models\Business
 * @author    Yisheng Zheng <zhengys@boqii.com>
 * @copyright 2016-2019 zhengys Co. All Rights Reserved.
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License
 * @version   GIT:<git_id>
 * @link      http://shop.openapi.boqii.com
 */
namespace App\Models\Common;

use App\Models\Model;
use Cache;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Config;

class City extends Model
{
    /**
     * 城市表
     *
     * @var string
     */
    protected $table = 'e_pro';
	
	protected static $_instance = null;
	
	/**
	 * 单例模式
	 *
     * @access public
     * @return mixed
     */
	public static function getInstance()
	{
		if (self::$_instance === null) {
            self::$_instance = new self();
        }
        return self::$_instance;
	}
	
	/**
	 * 获取城市
	 *
	 * @param Request $request
     * @access public
     * @return mixed
     */
	public function getCityList($request)
	{
		$where = [];
		$where[] = ['pro_status','=',0];
		$city_list = array();
		$pro = DB::table($this->table)->where($where)->get()->toArray();
		foreach($pro as $_p){
			$pro_data = array();
			$pro_data['id'] = $_p['id'];
			$pro_data['name'] = $_p['pro_name'];
			$pro_data['cities'] = array();
			$where = [];
			$where[] = ['pro_id','=',$_p['id']];
			$where[] = ['city_status','=',0];
			$city = DB::table('e_city')->where($where)->get()->toArray();
			foreach($city as $_c){
				$city_data = array();
				$city_data['id'] = $_c['id'];
				$city_data['p_id'] = $_p['id'];
				$city_data['name'] = $_c['city_name'];
				$city_data['district'] = array();
				$where = [];
				$where[] = ['city_id','=',$_c['id']];
				$where[] = ['district_status','=',0];
				$district = DB::table('e_district')->where($where)->get()->toArray();
				foreach($district as $_d){
					$district_data = array();
					$district_data['id'] = $_d['id'];
					$district_data['p_id'] = $_c['id'];
					$district_data['name'] = $_d['district_name'];
					$district_data['district'] = array();
					array_push($city_data['district'],$district_data);
				}
				array_push($pro_data['cities'],$city_data);
			}
			array_push($city_list,$pro_data);
		}
		return $city_list;
	}
}
